#!/usr/bin/env python
#go2.py
 

import images
import os.path
import pygtk
pygtk.require( "2.0" )
import gtk
from go2_view import View
from go2_model import Model
from go2_keyfunc import Go2_Keyfunc
import go2_utils # i/o, settings, and more

'''
Notes

	
'''

class Controller ( object ):
	def __init__(self,m,v):
		
		# why isn't this builder stuff in the view? isn't glade all view?
		
		builder = gtk.Builder()
		builder.add_from_file( "go2.glade" )

		self.io = go2_utils
		self.settings = go2_utils.go2_settings
		self.keyfunc = Go2_Keyfunc(m,v, self.settings)
		self.win   = builder.get_object( "window1" )
		
		v.set_io(self.io)
		v.set_wintitle(self.settings["wintitle"], self.win)
		v.set_treeview(builder.get_object( "treeview1" ))
		v.set_column1(builder.get_object( "treeviewcolumn1" ))
		v.set_cellpixbuf(builder.get_object( "cellrendererpixbuf1" ))
		v.set_modified_callback(self.set_modified)
		v.set_unsaved_dialog(builder.get_object("unsaved_dialog"))
		
		m.set_model(builder.get_object( "treestore1" ))
		
		builder.connect_signals( self )
		
		#in order to track when user clicks on row
		selection = v.treeview.get_selection()
		selection.connect('changed', self.on_selection_changed)

		self.win.connect("destroy", self.destroy_cb)
		self.win.connect('key_press_event', self.on_key_press_event)
		v.treeview.connect("button-press-event", self.treeview_clicked)
		
		m.load_model()
		v.make_view()
		
		self.win.show_all()
		return
		
	def destroy_cb(self, *kw):
	   """ Destroy callback to shutdown the app """
	   gtk.main_quit()
	   return
	 
	def run(self):
	   """ run is called to set off the GTK mainloop """
	   gtk.main()
	   return
	   
	# stub out menu item callbacks here
	# first, file menu
	def on_filemenu_new_activate(self, data):
		self.io.info("got here to new")
		
	def on_filemenu_open_activate(self, data):
		self.io.info("got here to open")
		if(self.settings["modified"]):
			self.io.info("opening over modified file!")
			#result = v.open_unsaved_dialog()
			self.io.info("result is %s" % result)
		filepath = v.open_filechooser()
		if(filepath):
			treestore = gtk.TreeStore(str)
			m.set_model (self.io.read_file(filepath, treestore))
		
			v.treeview.set_model() #sets model to none
			v.treeview.set_model(m.treestore) #assigns new model
			self.settings["filepath"] = filepath
			self.settings["wintitle"] = os.path.basename(filepath)
			v.set_wintitle(self.settings["wintitle"], self.win)
		
	def on_filemenu_save_activate(self, data):
		self.io.info("got here to save")
		filepath = go2_settings["filepath"]
		if(filepath):
			self.io.save_file(m.treestore, filepath)
		else:
			self.on_filemenu_saveas_activate(None)
			
	def on_filemenu_saveas_activate(self, data):
		self.io.info("got here to save as")
		filepath = v.saveas_filechooser()
		if(filepath):
			self.io.save_file(m.treestore, filepath)
			
	def on_filemenu_quit_activate(self, data):
		self.io.info("got here to quit")
		#check for modified file
		if(self.settings["modified"]):
			self.io.info("quitting with modified file!")
			pass
			#need to implement dialog in view
		
		gtk.main_quit()
		
	# edit menu callbacks
	def on_editmenu_cut_activate(self, data):
		self.io.info("editmenu cut")
	def on_editmenu_copy_activate(self, data):
		self.io.info("editmenu copy")
	def on_editmenu_paste_activate(self, data):
		self.io.info("editmenu paste")
	def on_editmenu_delete_activate(self, data):
		self.io.info("editmenu delete")
	def on_helpmenu_about_activate(self, data):
		self.io.info("helpmenu about")
	
	# outline menu callbacks
	def on_outline_indent_activate(self, data):
		self.keyfunc.load_event(None)
		v.indent(self.keyfunc.treestore, self.keyfunc.treeiter)
	def on_outline_outdent_activate(self, data):
		self.keyfunc.load_event(None)
		v.outdent(self.keyfunc.treestore, self.keyfunc.treeiter)
	def on_outline_up_activate(self, data):
		self.keyfunc.load_event(None)
		v.move_up(self.keyfunc.treestore, self.keyfunc.treeiter)
	def on_outline_dn_activate(self, data):
		self.keyfunc.load_event(None)
		v.move_down(self.keyfunc.treestore, self.keyfunc.treeiter)
	def on_outline_insert_activate(self, data):
		self.keyfunc.load_event(None)
		v.new_node(self.keyfunc.treestore, self.keyfunc.treeiter)
	def on_outline_expand_activate(self, data):
		self.keyfunc.load_event(None)
		v.treeview.expand_row( self.keyfunc.path, False )
	def on_outline_collapse_activate(self, data):
		self.keyfunc.load_event(None)
		v.treeview.collapse_row( self.keyfunc.path )
	def on_outline_expand_all_activate(self, data):
		pass
	def on_outline_collapse_all_activate(self, data):
		pass

	def on_selection_changed(self, selection): 
		self.io.info("selection changed")
		(model, iter) = selection.get_selected()
		if iter:
			# do the thing!
			#self.v.focus(model, iter)
			pass
		
	def treeview_clicked(self, widget, event):
		if event.button == 1 and event.type == gtk.gdk._2BUTTON_PRESS:
			self.io.info("Double clicked on treeview")
			
			self.keyfunc.load_event(event)
			self.keyfunc.map_doubleclick()
			return False
			
	def set_modified(self):
		# homemade callback, is called from view when user dirties buffer
		self.settings["modified"] = True
		t = self.settings["wintitle"]
		t = "%s*" % t
		v.set_wintitle(t, self.win)
		
	def on_key_press_event(self, widget, event):
		keyname = gtk.gdk.keyval_name(event.keyval)

		self.io.info("Key %s (%d) was pressed" % (keyname, event.keyval))
		# NOTE: no need to add keypresses to a manual buffer!
		#self.editbuffer is a gtk.TextBuffer

		#must use this line prior to calling any functions in keyfunc!
		self.keyfunc.load_event(event)
		
		#ready for mapping keys to actions
		#note - should convert to keycodes instead for language or are keynames 
		#consistent across diff languages?
		func = getattr(self.keyfunc, 'map_' + keyname, None)
		if func:
			return func()
		else:
			return self.keyfunc.map_default()

   	   
	
def main():
   gtk.main()
 
if __name__ == "__main__":
	m = Model()
	v = View(m)
	c = Controller(m,v)
	c.run()
